/**
 * 功能描述：日期时间选择器控制器
 * @author jin xiaohang
 * @date 2022/9/21 10:22
 */

class YwBaseDateTimePickerController{


    static instance(props){
        return new YwBaseDateTimePickerController(props)
    }

    /**
     * view视图
     * @type {null}
     * @private
     */
    _vueView =null

    /**
     * 安装自己
     * @param thisView {Object} vue视图
     */
    mounted(thisView){
        this._vueView =thisView
        let className = this.formatConversion(thisView.$options.name)
        this.recursiveAddChildrenClassName(className,thisView.$el)
    }

    /**
     * 格式化字符串（驼峰转下划线）
     * @param str {String}  待转字符串
     * @returns {string}
     */
    formatConversion(str){
        return str.replace(/([a-zA-Z])([A-Z])/g, '$1-$2').toLowerCase();
    }

    /**
     * 格式化字符串（下划线转驼峰）
     * @param str {String}待转字符串
     * @param num {number}  转换位置
     * @returns {string} 返回结果
     */
    formatConversionComponentName(str,num =0) {
        const arr = str.split('-');
        for (let index = 0; index < arr.length; index += 1) {
            if (index === 0 && num === 1) {
                arr[index] = arr[index].charAt(0).toLowerCase() + arr[index].substring(1).toLowerCase();
            } else {
                arr[index] = arr[index].charAt(0).toUpperCase() + arr[index].substring(1).toLowerCase();
            }
        }
        return arr.join('');
    }

    /**
     * 递归增加class类名
     * @param className {String}  类名
     * @param domCollection {object}   dom集合
     */
    recursiveAddChildrenClassName(className,domCollection){
        let childNodes  = domCollection.childNodes
        let name = this.formatConversionComponentName(className)
        if (this._vueView.$options.name === name){
            let tagNameConstant = domCollection.localName
            let firstLayoutName = className + '-'+tagNameConstant + '-' + 'layout'
            let firstStyleName = className + '-'+tagNameConstant + '-' + 'style'
            domCollection.classList.add(firstLayoutName)
            domCollection.classList.add(firstStyleName)
        }
        for (let index = 0; index < childNodes.length; index++) {
            let tagName = childNodes[index]
            let tagNameConstant = tagName.localName
            let layoutName = className + '-' + tagNameConstant + '-' + 'layout'
            let styleName = className + '-' + tagNameConstant + '-' + 'style'
            tagName.classList.add(layoutName)
            tagName.classList.add(styleName)
            className = className + '-' + tagNameConstant
            this.recursiveAddChildrenClassName(className,tagName)
        }
    }



}
export default YwBaseDateTimePickerController;